package org.example.base;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public interface ResponseJson {

    default void outputJson(HttpServletResponse response, Object data) {
        response.setHeader("Content-Type", "application/json;charset=utf-8");
        String jsonString = JSONObject.toJSONString(data);
        try {
            PrintWriter writer = response.getWriter();
            writer.write(jsonString);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
